package com.hzy.admin.services.system.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzy.admin.services.system.ICCTService;
import com.hzy.admin.models.vo.system.TableInfoTreeVo;
import com.hzy.admin.repository.mapper.system.DataBaseMapper;
import com.hzy.admin.repository.entity.system.SysTableInfo;
import com.hzy.admin.repository.entity.system.SysTableSchema;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 代码生成器
 *
 * @author hzy
 */
@Service
public class CCTServiceImpl extends ServiceImpl<DataBaseMapper, SysTableSchema> implements ICCTService {

    private static List<SysTableInfo> sysTableInfos = new ArrayList<>();
    private static List<SysTableSchema> sysTableSchemas = new ArrayList<>();

    public CCTServiceImpl() {

    }

    @Override
    public List<SysTableSchema> getTables() {
        return this.baseMapper.getTables();
    }

    @Override
    public List<SysTableInfo> getAllTableInfo() {
        return this.baseMapper.getTableInfoByName(null);
    }

    @Override
    public List<SysTableInfo> getTableInfoByName(String tableName) {
        return this.baseMapper.getTableInfoByName(tableName);
    }

    @Override
    public void init() {
        sysTableInfos = this.getAllTableInfo();
        sysTableSchemas = this.getTables();
    }

    @Override
    public List<TableInfoTreeVo> getTableInfoTree() {
        ArrayList<TableInfoTreeVo> res = new ArrayList<>();
        if (sysTableSchemas.isEmpty()) {
            this.init();
        }
        for (SysTableSchema item : sysTableSchemas) {
            TableInfoTreeVo tableInfoTreeVo = new TableInfoTreeVo();
            tableInfoTreeVo.setLabel(item.getName());
            tableInfoTreeVo.setComment(item.getComment());
            tableInfoTreeVo.setRank(1);
            tableInfoTreeVo.setType(item.getType());
            List<SysTableInfo> tableinfos = sysTableInfos.stream().filter(w -> w.getTableName().equals(item.getName()))
                    .collect(Collectors.toList());
            for (SysTableInfo tableInfo : tableinfos) {
                TableInfoTreeVo tableInfoTreeVoChildren = new TableInfoTreeVo();
                tableInfoTreeVoChildren.setLabel(tableInfo.getName());
                tableInfoTreeVoChildren.setComment(tableInfo.getComment());
                tableInfoTreeVoChildren.setRank(2);
                tableInfoTreeVoChildren.setType(tableInfo.getType());
                tableInfoTreeVo.getChildren().add(tableInfoTreeVoChildren);
            }
            res.add(tableInfoTreeVo);
        }
        return res;
    }

    @Override
    public String createModel(String tableName, String tempCode) {
        List<SysTableInfo> tableInfos = sysTableInfos.stream().filter(w -> w.getTableName().equals(tableName))
                .collect(Collectors.toList());
        StringBuilder fields = new StringBuilder();

        for (SysTableInfo item : tableInfos) {
            if (item.getName().equals("id") || item.getName().equals("createTime")
                    || item.getName().equals("updateTime")) {
                continue;
            }

            // https://www.cnblogs.com/henuyuxiang/p/6676660.html
            String type = "String";
            switch (item.getType()) {
                case "datetime":
                case "timestamp":
                    type = "LocalDateTime";
                    break;
                case "float":
                    type = "Float";
                    break;
                case "double":
                    type = "Double";
                    break;
                case "decimal":
                    type = "BigDecimal";
                    break;
                case "bit":
                    type = "Boolean";
                    break;
                case "integer":
                    type = "Long";
                    break;
                case "int":
                    type = "Integer";
                    break;
                case "varchar":
                default:
                    type = "String";
                    break;
            }

            fields.append("\tprivate " + type + " " + item.getName() + ";");
        }

        tempCode = tempCode.replace("<#className#>", tableName);
        tempCode = tempCode.replace("<#tableName#>", tableName);
        tempCode = tempCode.replace("<#fileds#>", fields.toString());

        return tempCode;
    }

}
